import 'package:flutter/material.dart';

Widget confirmDialog(
  String content,
  VoidCallback onConfirmPressed,
  VoidCallback onCancelPressed, {
  bool hasDivider = true,
  String confirmText = "确定",
  String cancelText = "取消",
  double contentSize = 16,
  double confirmSize = 16,
  double cancelSize = 16,
  Color? background = Colors.white,
  Color? contentColor = const Color(0xff333333),
  Color? confirmColor = const Color(0xff333333),
  Color? cancelColor = const Color(0xff999999),
  double width = 240,
  double contentHeight = 80,
  double optionBtnHeight = 40,
  double radius = 10,
  bool singleBtn = false,
}) {
  double height = 0;
  if (hasDivider) {
    height = contentHeight + optionBtnHeight + 0.5;
  } else {
    height = contentHeight + optionBtnHeight;
  }
  return Container(
    width: width,
    height: height,
    decoration: BoxDecoration(
      color: background,
      borderRadius: BorderRadius.circular(radius),
    ),
    clipBehavior: Clip.antiAlias,
    alignment: Alignment.center,
    child: Column(
      children: [
        _dialogContent(contentHeight, content, contentSize, contentColor),
        Visibility(visible: hasDivider, child: const Divider(height: 0.5)),
        _dialogOption(
          hasDivider,
          singleBtn,
          optionBtnHeight,
          confirmText,
          cancelText,
          confirmSize,
          cancelSize,
          confirmColor,
          cancelColor,
          onConfirmPressed,
          onCancelPressed,
        ),
      ],
    ),
  );
}

Widget _dialogOption(
  bool hasDivider,
  bool singleBtn,
  double contentHeight,
  String confirmText,
  String cancelText,
  double confirmSize,
  double cancelSize,
  Color? confirmColor,
  Color? cancelColor,
  VoidCallback onConfirmPressed,
  VoidCallback onCancelPressed,
) {
  return singleBtn
      ? SizedBox(
          height: contentHeight,
          child: _dialogButton(
            confirmText,
            confirmSize,
            confirmColor,
            contentHeight,
            onConfirmPressed,
          ),
        )
      : SizedBox(
          height: contentHeight,
          child: Row(
            children: [
              Expanded(
                child: _dialogButton(
                  cancelText,
                  cancelSize,
                  cancelColor,
                  contentHeight,
                  onCancelPressed,
                ),
              ),
              Visibility(
                visible: hasDivider,
                child: const VerticalDivider(width: 0.5),
              ),
              Expanded(
                child: _dialogButton(
                  confirmText,
                  confirmSize,
                  confirmColor,
                  contentHeight,
                  onConfirmPressed,
                ),
              ),
            ],
          ),
        );
}

Widget _dialogContent(
  double contentHeight,
  String content,
  double fontSize,
  Color? foregroundColor,
) {
  return Container(
    height: contentHeight,
    alignment: Alignment.center,
    child: Text(
      content,
      style: TextStyle(fontSize: fontSize, color: foregroundColor),
    ),
  );
}

Widget _dialogButton(
  String data,
  double fontSize,
  Color? foregroundColor,
  double height,
  VoidCallback onPressed,
) {
  return MaterialButton(
    onPressed: onPressed,
    minWidth: double.infinity,
    height: height,
    padding: EdgeInsets.zero,
    child: Text(
      data,
      style: TextStyle(fontSize: fontSize, color: foregroundColor),
    ),
  );
}
